/*
  The MIT License
  
  Copyright (c) 2009 smath project
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/
package pl.smath.expression.model;

/**
 * In (belongs to) term x \in A
 * 
 * @author Krzysztof Langner
 *
 */
public class InTerm extends Term{

	private final Term element;
	private final Term set;
	
	/**
	 * Construktor
	 * @param text
	 */
	public InTerm(Term element, Term set) {
		
		super(" \\in ");
		this.element = element;
		this.set = set;
	}
	
	
	/**
	 * @return tekst wyrażenia
	 */
	@Override
	public Term copy(){
		Term term = new InTerm(element.copy(), set.copy());
		
		term.setNegative(isNegative());
		return term;
	}

	
	
	/**
	 * Pobranie elementu
	 */
	public Term getElement(){
		return element;
	}

	
	/**
	 * Pobranie zbioru
	 */
	public Term getSet(){
		return set;
	}
	
	
	/**
	 * Ten term nie jest liczba
	 */
	@Override
	public boolean isNumber() {
		return false;
	}
	
	
	/**
	 * Sprawdzenie czy 2 wyrazenia sa sobie równowazne.
	 * Czyli czy mozna postawic miedzy nimi znak = (w sensie matematycznym)
	 */
	@Override
	public boolean isAbsTheSame(Term term){
		
		if(term instanceof InTerm){
			InTerm ft = (InTerm)term;
			return element.isAbsTheSame(ft.getElement()) && set.isAbsTheSame(ft.getSet());
		}
		
		return false;
	}
	
	
	/**
	 * Zamien na string
	 */
	@Override
	public String toString(){
		String output;
		
		output = element.toString() + getText() + set.toString();
		
		return output;

	}
	
	
	/**
	 * Transformacja wezla
	 */
	@Override
    public Term transform(ITransform t) {
		Term outputTerm = null;
        Term firstTerm = null;
        Term secondTerm = null;

        outputTerm = t.process(this);
        
        if(outputTerm == this){

        	firstTerm = element.transform(t);
        	secondTerm = set.transform(t);
                
            // Jezeli zmienily sie dzieci to tworzony jest nowy wezel
        	if(firstTerm != element || secondTerm != set){
        		outputTerm = new InTerm(firstTerm, secondTerm);
        		outputTerm.setNegative(isNegative());
        	}
        }
        else{
        	outputTerm = outputTerm.transform(t);
        }
        
        return outputTerm;
    }

}
